package com.kfh.ybracelet.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.utils.DensityUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 订单列表
 * Created by Aaron on 16/10/8.
 */
public class OrderHistoryAdapter extends BaseAdapter {

    private Context ctx;
    private JSONArray cardList;

    private final static int RECHARGE_SUCCESS = 1;//已经支付给运营商,且运营商确认
    private final static int RECHARGEING = -1;//未支付给运营商
    private final static int RECHARGE_WAITING = 0;//已经支付给运营商,但是需要运营商确认

    public OrderHistoryAdapter(Context ctx, JSONArray cardList) {
        this.ctx = ctx;
        this.cardList = cardList;
    }

    @Override
    public int getCount() {
        return cardList.length();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_order_info, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_order_info = (TextView) convertView.findViewById(R.id.tv_order_info);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        try {
            if (cardList == null || cardList.get(position) == null)
                return convertView;

            JSONObject o = (JSONObject) cardList.get(position);

        /*
       "id": 1,
            "phoneNum": "15311280989",
            "tradeNo": "20161008142629416456705932806265",
            "cardTime": 12,
            "subject": "服务费：一年套餐（含每月30分钟通话，30M流量）",
            "addTime": null,
            "state": "-1"
         */
//            o.getString("phoneNum");
//            o.getString("tradeNo");
//            o.getString("cardTime");
//            o.getString("subject");
//            o.getString("state");

            viewHolder.tv_order_info.setTextSize(12);
            viewHolder.tv_order_info.setPadding(DensityUtil.dip2px(ctx, 20), 0, DensityUtil.dip2px(ctx, 20), 0);

            String recharge_status = "";

            switch (o.getInt("state")) {

                case RECHARGE_SUCCESS:
                    recharge_status = "充值成功";
                    break;
                case RECHARGEING:
                    recharge_status = "充值中";
                    break;
                case RECHARGE_WAITING:
                    recharge_status = "充值中";
                    break;
                default:
                    break;
            }

            viewHolder.tv_order_info.setText(
                    "订单号:" + o.getString("tradeNo")
                            + "\n充值手机号:" + o.getString("phoneNum")
//                        +    "\n充值时间:" + CommonUtils.getLocalTime(o.getString("addTime")) +
                            + "\n充值时间:" + o.getString("addTime")
                            + "\n充值套餐:" + o.getString("subject")
//                            + "\n充值状态:" + recharge_status
            );

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return convertView;
    }

    private class ViewHolder {

        private TextView tv_order_info;


    }
}
